package com.hschinese.pronunciation.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.TextView;

import com.hschinese.pronunciation.R;
import com.hschinese.pronunciation.utils.NumberFormatUtil;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import studychinese.adapters.GvResultPinyinAdapter;
import studychinese.fragment.BaseFragment;
import studychinese.model.TextRecultBean;
import studychinese.model.UserBean;


public class ResultPinyinFragment extends BaseFragment {

    @Bind(R.id.lv_result_pinyin)
    GridView lvResultPinyin;
    @Bind(R.id.fl_native_whystuday_cz)
    TagFlowLayout flNativeWhystudayCz;
    @Bind(R.id.tv_head_resultpinyin)
    TextView tvHeadResultpinyin;
    private TagAdapter<String> mAdapter;
    private LayoutInflater mInflater;

    public ResultPinyinFragment() {
        // Required empty public constructor
    }

   private int rightTimes;
    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_result_pinyin, container, false);
        ButterKnife.bind(this, view);
        mInflater = inflater;
        lvResultPinyin.setFocusable(false);
        TextRecultBean tetResultInfor = UserBean.getTetResultInfor();

        List<TextRecultBean.DataBean> data = tetResultInfor.getData();
        if (tetResultInfor.getTotalcount() != tetResultInfor.getState()) {
            data.clear();
            tvHeadResultpinyin.setText("0%");
        }else {
            for (int i = 0; i <data.size() ; i++) {
                if(data.get(i).isBooleanFlag()){
                    rightTimes++;
                }
            }
            String percent = NumberFormatUtil.getPercent(rightTimes-1, data.size());
            tvHeadResultpinyin.setText(percent+"%");
        }
        GvResultPinyinAdapter gvResultPinyinAdapter = new GvResultPinyinAdapter(data, getActivity());
        lvResultPinyin.setAdapter(gvResultPinyinAdapter);
        lvResultPinyin.setOverScrollMode(View.OVER_SCROLL_NEVER);
        setView(data);
        return view;
    }


    private void setView(List<TextRecultBean.DataBean> listData) {
        ArrayList<String> list = new ArrayList<>();
        list.clear();
        for (int d = 0; d < listData.size(); d++) {
            if (!listData.get(d).isBooleanFlag()) {
                list.add(listData.get(d).getRightPy());
            }


        }
        flNativeWhystudayCz.setAdapter(mAdapter = new TagAdapter<String>(list) {

            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) mInflater.inflate(R.layout.tv_result_pinyin,
                        flNativeWhystudayCz, false);

                tv.setText(s);
                return tv;
            }
        });
        mAdapter.setSelectedList();
        //获得所有选中的pos集合
        flNativeWhystudayCz.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                //Toast.makeText(getActivity(), mVals[position], Toast.LENGTH_SHORT).show();
                //view.setVisibility(View.GONE);
                return true;
            }
        });


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }



}
